<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/6
 * Time: 17:27
 */

namespace app\modules\backed\controllers;

use app\models\form\ArticleForm;
use app\models\sql\ArticleModel;
use yii\data\Pagination;


class ArticleController extends BaseController
{

    /**
     * 文章列表
     */
    public function actionIndex()
    {
        return $this->render('list');
    }
    public function actionData()
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $query = ArticleModel::find()->where(['status' => 0]);
        $count = $query->count();
        $request = \Yii::$app->request;
        $postData = $request->get();

        $pagination = new Pagination(['totalCount' => $count,'defaultPageSize'=>$postData['rows']?:0]);
        $articles = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        //var_dump($pagination);
        return ['rows'=>$articles,'total'=>$count,'page'=>$pagination];
    }

    /**
     * 文章列表
     */
    public function actionAddView()
    {
        return $this->render('add');
    }
    public function actionAdd()
    {

        $article = new ArticleModel();
        $article->load(\Yii::$app->request->post(),'');
        $session = \Yii::$app->session;
        $article->account_id = $session->get('account_id');
       // $articleForm->attributes = \Yii::$app->request->post('ArticleForm');
        //var_dump($articleForm);
        if($article->validate()){
            $article->save();
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return ['code'=>1,'msg'=>'增加成功'];
        }else{
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return ['code'=>2,'msg'=>$article->firstErrors] ;
        }
    }

    public function actionUpdateView()
    {
        
    }
}